package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.RoleMapper;
import com.qf.pojo.RolePojo;
import com.qf.service.RoleService;
import com.qf.utils.Constant;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RolePojo> implements RoleService {

    @Autowired
    RoleMapper roleMapper;
    @Override
    @Transactional
    public ResultMsg add(RolePojo rolePojo) {
        boolean save = this.save(rolePojo);
        if (save)
            return ResultMsg.SUCCESS();
        return ResultMsg.FAILED(Constant.WB,"添加角色信息失败");
    }

    //查询全部角色
    @Override
    public ResultMsg findAll(String keyword) {
        QueryWrapper<RolePojo> wrapper = new QueryWrapper<>();
        wrapper.like("role_name",keyword);
        List<RolePojo> list = this.list(wrapper);
        if (list!=null)
            return ResultMsg.SUCCESS(list);
        return ResultMsg.FAILED(Constant.WB,"查询失败");
    }

    //根据id删除角色信息
    @Override
    @Transactional
    public ResultMsg delById(Integer roleId) {
        boolean b = this.removeById(roleId);
        if (b)
            return ResultMsg.SUCCESS();
        return ResultMsg.FAILED(Constant.WB,"删除失败");
    }

    //修改角色信息
    @Override
    @Transactional
    public ResultMsg update(RolePojo rolePojo) {
        UpdateWrapper<RolePojo> wrapper = new UpdateWrapper<>();
        boolean b = this.updateById(rolePojo);
        if (b)
            return ResultMsg.SUCCESS(rolePojo);
        return ResultMsg.FAILED(Constant.WB,"修改失败");
    }
}
